//
//  OrderWuLiaoViewController.m
//  MaJangJi
//
//  Created by 嗯，大葱 on 16/9/11.
//  Copyright © 2016年 嗯，大葱. All rights reserved.
//

#import "OrderWuLiaoViewController.h"
#import "OrderWuLiaoTableViewCell.h"
#import "Third10TableViewCell.h"
#import "Third11TableViewCell.h"
#import "FangAn1TableViewCell.h"
#import "FN4TableViewCell.h"
#import "FN5TableViewCell.h"
#import "AddressViewController.h"
#import "Header.h"
#define k_Width      [UIScreen mainScreen].bounds.size.width

#define k_Height     [UIScreen mainScreen].bounds.size.height
@interface OrderWuLiaoViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign) NSInteger num;

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *tel;
@property (nonatomic, copy) NSString *address;



@end

/*
 人之初	性本善	性相近	习相远
 首孝悌	次见闻	知某数	识某文
 */

@implementation OrderWuLiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"MRAddress"] != nil) {
        self.address = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRAddress"];
        self.name = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRName"];
        self.tel = [[NSUserDefaults standardUserDefaults] objectForKey:@"MRTel"];
    }
    
    [self setNaVcBar];
    [self configureViewController];
}

- (void)setNaVcBar{
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, -20, k_Width, 20)];
//    statusBarView.backgroundColor=[UIColor colorWithRed:82/255.0 green:82/255.0 blue:82/255.0 alpha:1.0];
//    [self.navigationController.navigationBar addSubview:statusBarView];
    
    
    self.navigationItem.title = @"选择物料";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStylePlain) target:self action:@selector(handleBack2)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor grayColor];

    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"问"] style:(UIBarButtonItemStylePlain) target:self action:@selector(hanldeRightBtnAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];

}
#pragma mark 导航栏按钮点击事件
- (void)handleBack2 {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)hanldeRightBtnAction {
 
    
    
}
#pragma mark 配置控制器视图

- (void)configureViewController {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, k_Width, k_Height - 30) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderWuLiaoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell13"];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

//    self.tableView.allowsSelection = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"Third10TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell10"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"Third11TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell11"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FangAn1TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FN4TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FN5TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell5"];
    
    
    UIButton *sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sureBtn.backgroundColor = [UIColor redColor];
    sureBtn.frame = CGRectMake(0, k_Height - 50, k_Width, 50);
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [sureBtn setTitle:@"确认发布订单" forState:(UIControlStateNormal)];
    [sureBtn addTarget:self action:@selector(handleSureBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:sureBtn];
    
    [self create];
    
}
-(void)create{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=InsertCustomized&Size=定制面积&userid=%@&addressid=地址ID【getaddress接口返回的ID",USERID] success:^(id result) {
    
    
    } error:^(NSError *error) {
    
    
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 1;
    }
    if (section == 2) {
        return 2;
    }
    if (section == 3) {
        return 5;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        Third10TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell10"];
        return cell;
        
    }
    else if ( indexPath.section == 1) {
        Third11TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell11"];
        cell.getPersonLabel.text = self.name;
        cell.phoneLabel.text = self.tel;
        cell.addressLabel.text = self.address;
        return cell;
    }
    else if ( indexPath.section == 2) {
        FangAn1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        return cell;
    }
    else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            FN5TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell5"];
            return cell;
        }else {
            OrderWuLiaoTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell13"];
            return cell;
        }
    }
    else {
        OrderWuLiaoTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell13"];
        return cell;
    }
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 77;
    }
    else {
        return 40;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==1 ) {
        AddressViewController *add = [[AddressViewController alloc] init];
        [add blockstring:^(NSInteger str, NSString *address, NSString *name, NSString *tel) {
            
            self.address = address;
            self.name = name;
            self.tel  = tel;
            [self.tableView reloadData];
            
        }];
        [self.navigationController pushViewController:add animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)handleSureBtnAction:(UIButton *)sender {
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=InsertCustomized&Size=%@&userid=%@&addressid=%ld",self.mian,USERID,self.num] success:^(id result) {

        if ([[result[0] objectForKey:@"msg"] isEqualToString:@"success"]) {
            
//            [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=InsertCustomizedDetail& Giftid=商品平台ID&Giftdetailid=商品具体型号id&count=定制数量&Customizedid=%@&userid=%@",self.mian,USERID] success:^(id result) {
//                
//                
//                
//            } error:^(NSError *error) {
//                
//                
//            }];
        }
        
    } error:^(NSError *error) {
        
        
    }];
    NSLog(@"我是确认发布订单按钮");
}
@end
